package com.bolt.admin.web.controller;

import com.bolt.admin.module.email.dto.EmailConfigDTO;
import com.bolt.admin.module.email.dto.converter.EmailConfigConverter;
import com.bolt.admin.module.email.entity.EmailConfigEntity;
import com.bolt.admin.module.email.service.EmailService;
import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.support.spring.jpa.specification.QueryHelp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2020-10-30 14:30:17
 *         邮箱配置
 **/
@RestController
@RequestMapping("/api/email")
@RequiredArgsConstructor
@Api(description = "邮箱配置")
public class EmailConfigController {

    private final EmailService emailService;

    /**
     * 邮箱配置列表分页
     *
     * @return
     */
    @PostMapping(value = "/page")
    @ApiOperation(notes = "邮箱配置列表分页邮箱配置列表分页", httpMethod = "GET", value = "邮箱配置列表分页")

    public ResultMessage list(Pageable pageable, @RequestBody EmailConfigDTO dto) {
        PageInfo <EmailConfigEntity> pageInfo = emailService.findPage(pageable, (root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, dto, criteriaBuilder));
        return Results.success(pageInfo.map(K -> EmailConfigConverter.toDTO(K)));

    }

    /**
     * 根据ID邮箱配置操作
     */
    @GetMapping("/get/{id}")
    @ApiOperation(notes = "根据ID查询邮箱配置", httpMethod = "GET", value = "根据ID查询邮箱配置")
    public ResultMessage <EmailConfigDTO> get(@PathVariable Long id) {
        EmailConfigEntity configEntity = emailService.findOne(id).get();
        return Results.success(EmailConfigConverter.toDTO(configEntity));
    }

    /**
     * 邮箱配置编辑操作
     *
     * @param dao 表单数据
     * @return
     */
    @PostMapping(value = "/saveOrUpdate")
    @ApiOperation(notes = "邮箱配置编辑", httpMethod = "POST", value = "邮箱配置编辑")
    public ResultMessage <Void> saveOrUpdate(@Validated @RequestBody EmailConfigDTO dao) {
        emailService.saveOrUpdate(dao);
        return Results.success();
    }

    /**
     * 邮箱配置删除操作
     */
    @PostMapping(value = "/del")
    @ApiOperation(notes = "根据ID删除邮箱配置", httpMethod = "POST", value = "根据ID删除邮箱配置")
    public ResultMessage <Void> doDelete(@RequestParam("id") Long id) {
        emailService.deleteById(id);
        return Results.success();
    }


}